package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.ShopAdapter;
import com.lc.afbx.conn.GetApiShowStore;
import com.lc.afbx.conn.PostSubmitApply;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PictureUtil;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.widget.MyListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilKeyBoard;
import com.zcx.helper.util.UtilNetWord;
import com.zcx.helper.util.UtilToast;

import java.util.List;

/**
 * Created by Administrator on 2016/7/4.
 */
public class AccidentInsuranceInsureActivity extends BaseActivity implements View.OnClickListener {

    private EditText accident_insurance_insure_managerCode_et;
    private List<GetApiShowStore.Data> dataList;
    private ShopAdapter adapter;
    private MyListView accident_insurance_insure_lv;

    private Bundle bundle;
    private ImageView accident_insurance_insure_pay_iv;

    private int payTpye = 1; //支付类型 1  银联支付，只有银联啊

    private int selectPosition = -1;


    // 这是保险门店的接口
    private GetApiShowStore getApiShowStore = new GetApiShowStore(new AsyCallBack<GetApiShowStore.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiShowStore.Info info) throws Exception {

            dataList = info.dataList;
            accident_insurance_insure_lv.setAdapter(adapter = new ShopAdapter(context, info.dataList));
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    private PostSubmitApply postSubmitApply = new PostSubmitApply(new AsyCallBack<PostSubmitApply.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostSubmitApply.Info info) throws Exception {

            UtilToast.show(context, "投保成功");

            Intent intent = new Intent(context, PayWebActivity.class);
                intent.putExtra("ordersn", info.ordersn);
                intent.putExtra("type", "3");

                startActivity(intent);

            BaseApplication.INSTANCE.finishActivity(AccidentInsuranceInsureActivity.class, AccidentInsuranceDetailActivity.class, AccidentInsuranceFillInfoActivity.class, AccidentInsuranceActivity.class);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accident_insurance_insure);

        bundle = getIntent().getExtras();

        ((TextView) findViewById(R.id.accident_insurance_insure_productName_tv)).setText(bundle.getString("insurance_type"));
        ((TextView) findViewById(R.id.accident_insurance_insure_money_tv)).setText(bundle.getString("premium") + "元");

        accident_insurance_insure_managerCode_et = ((EditText) findViewById(R.id.accident_insurance_insure_managerCode_et));
        accident_insurance_insure_pay_iv = ((ImageView) findViewById(R.id.accident_insurance_insure_pay_iv));

        if (!TextUtils.isEmpty(BaseApplication.BasePreferences.readCode())) {

            accident_insurance_insure_managerCode_et.setEnabled(false);
            accident_insurance_insure_managerCode_et.setText(BaseApplication.BasePreferences.readCode());

        }

        accident_insurance_insure_lv = ((MyListView) findViewById(R.id.accident_insurance_insure_lv));

        accident_insurance_insure_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                UtilLog.e("zhelia ===", "zoule");

                UtilKeyBoard.closeKeybord(accident_insurance_insure_managerCode_et, context);

                if (adapter.isCheck.get(position) == false) {
                    adapter.init();
                    adapter.isCheck.put(position, true);
                    selectPosition = position;
                    adapter.notifyDataSetChanged();
                } else {
                    adapter.isCheck.put(position, false);
                    selectPosition = -1;
                    adapter.notifyDataSetChanged();
                }
            }
        });


        getApiShowStore.cid = bundle.getString("insurance_id");
        getApiShowStore.curcode = BaseApplication.BasePreferences.readHomeCityID();
        getApiShowStore.execute(this);

        setAppCallBack(new closeCallback());


        findViewById(R.id.accident_insurance_insure_submit_bt).setOnClickListener(this);
        findViewById(R.id.accident_insurance_insure_pay_rl).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                break;

            case R.id.accident_insurance_insure_pay_rl:

                Log.e("---支付点击事件-->", "你看 好使");

                break;

            case R.id.accident_insurance_insure_submit_bt:

                String managerCode = accident_insurance_insure_managerCode_et.getText().toString().trim();

                if (selectPosition < 0) {
                    UtilToast.show(this, "请选择一个投保门店");
                    return;
                }

                String storeId = dataList.get(selectPosition).id;

//                UtilLog.e("投保人身份证路径---------", bundle.getString("applica_idUrl"));
//                UtilLog.e("投保人照片路径---------", bundle.getString("applica_peopleUrl"));

                postSubmitApply.applicant_information = bundle.getString("applicant_information");
                postSubmitApply.id_url = PictureUtil.bitmapToString(bundle.getString("applica_idUrl"));
                postSubmitApply.self_url = PictureUtil.bitmapToString(bundle.getString("applica_peopleUrl"));

                // 判断其他被保人是不是本人  不是本人 就那获取json  存图片 然后在提交  ，是本人 提交 空数据
                if (bundle.getString("relation_yuBeiBao").equals("2")) {

//                    UtilLog.e("其他投保人身份证路径---------", bundle.getString("other_idUrl"));
//                    UtilLog.e("其他投保人身份证路径---------", bundle.getString("other_peopleUrl"));

                    postSubmitApply.other_insureds = bundle.getString("other_insureds");
                    postSubmitApply.otherid_url = PictureUtil.bitmapToString(bundle.getString("other_idUrl"));
                    postSubmitApply.otherself_url = PictureUtil.bitmapToString(bundle.getString("other_peopleUrl"));

                } else {
                    postSubmitApply.other_insureds = "";
                    postSubmitApply.otherid_url = "";
                    postSubmitApply.otherself_url = "";
                }


                postSubmitApply.userid = BaseApplication.BasePreferences.readUserId();

                postSubmitApply.relation = bundle.getString("relation_yuBeiBao", "===========");
                postSubmitApply.death_beneficiary = bundle.getString("death_beneficiary", "===========");

                postSubmitApply.insurance_type = bundle.getString("insurance_type", "===========");
                postSubmitApply.copies = bundle.getString("copies", "===========");
                postSubmitApply.premium = bundle.getString("premium", "===========");
                postSubmitApply.payment_way = bundle.getString("payment_way", "===========");
                postSubmitApply.payment_is = bundle.getString("payment_is", "");

                postSubmitApply.cid = bundle.getString("insurance_id", "===========");
                postSubmitApply.stores_id = storeId;
                postSubmitApply.starttime = bundle.getString("starttime", "===========");
                postSubmitApply.time_limit = bundle.getString("time_limit", "");
                postSubmitApply.manager_code = managerCode;

                postSubmitApply.execute(this);


                Log.e("payment_way", bundle.getString("payment_way", "==========="));
                break;
        }

    }


    class closeCallback implements AppCallBack {

        public void close() {

            BaseApplication.INSTANCE.finishActivity(AccidentInsuranceFillInfoActivity.class, AccidentInsuranceDetailActivity.class, AccidentInsuranceInsureActivity.class);
        }

    }
}
